package com.project.fiveCerealsSavoury.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.project.fiveCerealsSavoury.domain.Order;
import com.project.fiveCerealsSavoury.domain.OrderState;
import com.project.fiveCerealsSavoury.domain.User;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.OrderService;
import com.project.fiveCerealsSavoury.transfer.Page;
import com.project.fiveCerealsSavoury.transfer.UserPage;

@Controller
public class OrderController {
	@Autowired
	private OrderService orderService;
	
	/**
	 * 根据用户id和订单状态查询
	 * @param userId
	 * @param orderstate
	 * @return
	 * @throws BizException 
	 */
	@RequestMapping("/queryStateOrder.do")
	public String queryStateOrder(HttpServletRequest request, String orderState,ModelMap modelMap) throws BizException{
		User user = (User) request.getSession().getAttribute("USER");
		if(user == null){
			return "/user/login";
		}
		if (orderState == null) {
			orderState= "已支付";
		}else{
			try {
				orderState =new String(orderState.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List<Order> queryList = orderService.queryStateOrder(user.getId(),OrderState.valueOf(orderState));
		int i = 0;
		if (queryList != null) {
			modelMap.put("queryList", queryList);
			if (orderState.equals(OrderState.已取消)) i = 1;
			if (orderState.equals(OrderState.已支付)) i = 2;
			if (orderState.equals(OrderState.未支付)) i = 4;
			if (orderState.equals(OrderState.已完成)) i = 3;
			modelMap.put("i", i);
			
		}
		return "/userIndex/orders";
	}

	/**
	 * 根据订单id和订单状态查
	 * @param id
	 * @param orderState
	 * @return
	 */
	@PostMapping("/order/queryById.do")
	public String queryById(Integer id, OrderState orderState,ModelMap modelMap){
		try {
			Order queryById = orderService.queryById(id, orderState);
			if (queryById != null) {
				return "/order/listById";
			}
		} catch (BizException e) {
			modelMap.put("exception", e.getLocalizedMessage());
			return "except";
		}
		return "/order/queryById";
	}
	
	/**
	 * 查询
	 * @param userPage
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/queryList.do")
	public String qorder(@Valid UserPage userPage, ModelMap modelMap,boolean isQuery,String orderState){
		if (orderState == null) {
			orderState= "已支付";
		}
		try {
			orderState =new String(orderState.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Page queryList = orderService.queryList(userPage,OrderState.valueOf(orderState));
		int i = 0;
		if (queryList != null) {
			modelMap.put("queryList", queryList);
			if (orderState.equals(OrderState.已完成)) i = 1;
			if (orderState.equals(OrderState.已支付)) i = 2;
			if (orderState.equals(OrderState.已取消)) i = 3;
			modelMap.put("i", i);
			if (isQuery) {
				return "/admin/indent";
			}
		}
		return "/userIndex/orders";
	}
	
	@RequestMapping("/delOrder.do")
	public String delOrder(Integer id, ModelMap modelMap){
		try {
			orderService.removeCar(id, OrderState.已添加到购物车);
		} catch (BizException e) {
			modelMap.put("exception", e.getLocalizedMessage());
			return "except";
		}
		return "redirect:/shopping.do";
	}
}
